package com.gold.web;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.gold.model.User;
import com.gold.service.UserService;
import com.gold.util.BaseController;
import com.gold.util.Constant;
import com.gold.util.MsgUtils;
import com.gold.util.RandomValidateCode;

/**
 * Created by wxf
 * 员工登录
 */
@Controller
@RequestMapping
public class StaffLoginContorller extends BaseController{

    @Resource
    private UserService userService;

    /**
     * 登陆页
     * @return
     */

    @RequestMapping(value = "/")
    //@RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView webIndex()
    {
        ModelAndView modelAndView = new ModelAndView("/login");
        return  modelAndView;
    }

    /**
     * 用户登陆请求
     * @param request
     * @return
     */
    @RequestMapping(value = "/staffLogin")
    public ModelAndView staffLogin(HttpServletRequest request,
                                  User user, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        try {
            User staffInfoTmp = getCurrentUser(request);
            modelAndView.addObject("loginname",user.getuName());
            if (staffInfoTmp != null) {
                modelAndView = new ModelAndView("redirect:/main");
            } else {
            	user=userService.selectByName(user.getuName(), user.getuPassword());
            	
            	if(user==null){
            		modelAndView.setViewName("/login");
            		modelAndView.addObject("msg","用户名或密码错误！");
            	}else{
            		modelAndView.setViewName("/main");
            		request.getSession().putValue(Constant.USER, userService.load(1));
            	}
            }

        } catch (Exception e) {
            modelAndView.addObject("msg","用户名或密码错误！");
            modelAndView.setViewName("/login");
            e.printStackTrace();
        }
        return  modelAndView;
    }


    /**
     * 验证码
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getImage" )
    public ModelAndView getImage(HttpServletRequest request,HttpServletResponse response) {
        response.setContentType("images/jpeg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request, response);//输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 首页
     * @return
     */

    @RequestMapping(value = "/main")
    public ModelAndView main(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject(request.getSession().getAttribute(Constant.USER));
        modelAndView.setViewName("main");
        return  modelAndView;
    }

    public List<Map<String,Object>> sortPermissions(List<Map<String,Object>> lists,String parent){
        List<Map<String,Object>> child=new ArrayList<Map<String, Object>>();
        for(Map<String,Object> list:lists){
            if(list.get("parentCode").toString().equals(parent) && list.get("flag")==null){
                //child.add();
                list.put("flag",true);
                List<Map<String,Object>> childrens=sortPermissions(lists,list.get("code").toString());
                if(childrens.size()>0){
                    list.put("children",childrens);
                }
                child.add(list);
            }
        }
        return child;
    }

    /**
     * 登陆页
     * @return
     */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public ModelAndView header(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        return  modelAndView;
    }


    /**
     * 用户退出登陆
     * @param request
     * @param response
     */
    @RequestMapping(value = "loginOut",method = RequestMethod.GET)
    public ModelAndView loginOut(HttpServletRequest request,HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        try {
            User staffInfo = getCurrentUser(request);
            if (staffInfo == null) {
                modelAndView = new ModelAndView("forward:/");
            } else {
                HttpSession session = request.getSession();
                session.removeAttribute(Constant.USER);
                //session.removeAttribute("validateCode");
                session.invalidate();
                modelAndView = new ModelAndView("forward:/");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return modelAndView;
    }
}
